package com.mud.dnd.frontend.web.mbeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.MeterGaugeChartModel;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.domain.model.Deck;
import com.mud.dnd.ejb.CardManager;
import com.mud.dnd.ejb.CommentManager;
import com.mud.dnd.ejb.DeckManager;


@ManagedBean
@SessionScoped
public class AdminPageBean {


	@EJB
	protected CardManager cardManager;
	
	@EJB
	protected DeckManager deckManager;
	
	@EJB
	protected CommentManager commentManager;
	
	
	private CartesianChartModel ratingChartModel; 
	private MeterGaugeChartModel cardCountModel;  
	private MeterGaugeChartModel deckCountModel;  
	
	private boolean renderFinished = false;
	
	@PostConstruct
	public void renderCharts()
	{
		createRatingChart();
		createCardCountChart();
		createDeckCountChart();
		renderFinished = true;
	}
	
	
	private void createRatingChart()
	{
		ratingChartModel = new CartesianChartModel();
		ChartSeries cardseries = new ChartSeries(); 
		cardseries.setLabel("Cards");
		
		for(double i=1; i<=5; i++)
			{
			List<QueryParameter> params = new ArrayList<QueryParameter>();
			try {
				
					params.add(new QueryParameter("rate",QueryParamOperator.EQUALS,i));
				}
			 catch (DAOException e) {
				showErrorMessage(e.getMessage());
			 }
			
			
			List<Card> cards = cardManager.getCards(params);
			cardseries.set(String.valueOf(i), cards.size());
			}
		
		ratingChartModel.addSeries(cardseries);
	}
	
	
	private void createCardCountChart()
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		List<Card> cards = cardManager.getCards(params);
		
		 List<Number> intervals = new ArrayList<Number>(){{  
	            add(1);  
	            add(30);  
	            add(500); 
	            add(1000);
	        }};  
	        
	     cardCountModel = new MeterGaugeChartModel(cards.size(), intervals);
		
	}
	
	private void createDeckCountChart()
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		List<Deck> decks = deckManager.getDecks(params);
		
		 List<Number> intervals = new ArrayList<Number>(){{  
	            add(1);  
	            add(30);  
	            add(500); 
	            add(1000);
	        }};  
	        
	     deckCountModel = new MeterGaugeChartModel(decks.size(), intervals);
		
	}
	
	
	
	
	protected void showErrorMessage(String msg)
	{
		
		 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", msg);  
		 FacesContext.getCurrentInstance().addMessage(null, message);  
	}

	public CartesianChartModel getRatingChartModel() {
		return ratingChartModel;
	}

	public void setRatingChartModel(CartesianChartModel ratingChartModel) {
		this.ratingChartModel = ratingChartModel;
	}


	public MeterGaugeChartModel getCardCountModel() {
		return cardCountModel;
	}


	public void setCardCountModel(MeterGaugeChartModel cardCountModel) {
		this.cardCountModel = cardCountModel;
	}


	public MeterGaugeChartModel getDeckCountModel() {
		return deckCountModel;
	}


	public void setDeckCountModel(MeterGaugeChartModel deckCountModel) {
		this.deckCountModel = deckCountModel;
	}


	public boolean isRenderFinished() {
		return renderFinished;
	}


	public void setRenderFinished(boolean renderFinished) {
		this.renderFinished = renderFinished;
	}
	
		
	
}
